<template>

    <!-- 添加或修改对话框 -->
    <lam-dialog :title="title" :visible.sync="open" width="680px" append-to-body @close="cancel">
      <el-form :ref="formRef" :model="form" :rules="rules" label-width="100px" :disabled="formDisabled">
        <el-row>
          <el-col :span="12">
            <el-form-item label="类型" prop="menuType">
              <lam-dict-select v-model="form.menuType" type="radio" dictCode="menu_type" @change="menuTypeChange" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="上级菜单" prop="parentId">
              <treeselect v-model="form.parentId" :options="treeData" :show-count="true" placeholder="选择上级菜单" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="标题" prop="name">
              <el-input v-model="form.name" clearable placeholder="" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="描述" prop="description">
              <el-input v-model="form.description" placeholder="" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12" v-if="form.menuType==0||form.menuType==1">
            <el-form-item label="跳转路径" prop="path">
              <span slot="label" v-if="form.menuType==1">
                跳转路径<el-tooltip content="注意: 该标识符首字母大写后必须与组件name属性相同, 否则页面组件缓存会失效" placement="top"><i class="el-icon-question"></i></el-tooltip>
              </span>
              <el-input v-model="form.path" clearable placeholder="" />
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="form.menuType==0||form.menuType==1">
            <el-form-item label="路径类型" prop="pathType">
              <lam-dict-select type="radio" v-model="form.pathType" dictCode="path_type" placeholder="" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="form.pathType==0">
          <el-col :span="24" v-if="form.menuType==1">
            <el-form-item label="组件" prop="component">
              <el-input v-model="form.component" placeholder="" />
            </el-form-item>
          </el-col>
          <el-col :span="24" v-if="form.menuType==1||form.menuType==2">
            <el-form-item prop="perms">
              <span slot="label">
                权限编码<el-tooltip content="多个用英文逗号分隔,每个权限前后的空格会自动去掉" placement="top"><i class="el-icon-question"></i></el-tooltip>
              </span>
              <el-input v-model="form.perms" type="textarea" autosize placeholder="" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12" v-if="form.menuType==0||form.menuType==1">
            <el-form-item label="菜单图标" prop="icon">
              <el-popover placement="bottom-start" width="600" trigger="click" @show="$refs['iconSelect'].reset()">
                <IconSelect ref="iconSelect" @selected="selected" />
                <el-input slot="reference" v-model="form.icon" placeholder="点击选择图标" clearable>
                  <svg-icon v-if="form.icon" slot="prefix" :icon-class="form.icon" class="el-input__icon"
                    style="height: 20px;width: 20px;vertical-align: -5px;" />
                  <i v-else slot="prefix" class="el-icon-search el-input__icon" />
                </el-input>
              </el-popover>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="form.menuType==0||form.menuType==1">
            <el-form-item label="隐藏路由" prop="hidden">
              <lam-dict-select type="radio" v-model="form.hidden" dictCode="yn10" placeholder="" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="排序" prop="sortNo">
              <el-input-number v-model="form.sortNo" clearable controls-position="right" :min="0" style="width: 100%;"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <lam-dict-select type="radio" v-model="form.status" dictCode="status_type" placeholder="" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :loading="loading">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </lam-dialog>

</template>

<script>
import { EditMixin } from '@/components/mixins/EditMixin';
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import IconSelect from "@/components/IconSelect";
import { menuTreeSelect } from "@/api/system/menu";

export default {
  name: "EditMenu",
  mixins:[EditMixin],
  components:{
    Treeselect,
    IconSelect
  },
  data() {
    return {
      // 表单校验
      rules: {
        name: [{ required: true, message: "标题不能为空", trigger: "blur" }, { validator: this.nameUniqueValidator, trigger: 'blur'}],
        path: [{ required: true, message: "跳转路径不能为空", trigger: "blur" }, { validator: this.pathUniqueValidator, trigger: 'blur'}],
        pathType: [{ required: true, message: "路径类型不能为空", trigger: "blur" }],
        component: [{ required: true, message: "组件不能为空", trigger: "blur" }],
        menuType: [{ required: true, message: "类型不能为空", trigger: "blur" }],
        perms: [{ required: true, message: "权限编码不能为空", trigger: "blur" }],
        sortNo: [{ required: true, message: "排序不能为空", trigger: "blur" }],
        hidden: [{ required: true, message: "是否隐藏路由不能为空", trigger: "blur" }],
        status: [{ required: true, message: "状态不能为空", trigger: "blur" }],
      },
      url: {
        base: '/sys/menu'
      },
      treeData:[],
      parentMenuType:'0',
    };
  },
  created() {
  },
  watch:{
    parentMenuType(newVal){
      if(this.open){
        this.loadMenuTree()
      }
    }
  },
  methods: {
    // 表单重置
    reset() {
      this.form = {
        parentId: '0',
        name: '',
        description: '',
        path: '',
        pathType: 0,
        component: '',
        menuType: 0,
        perms: '',
        sortNo: 10,
        icon: '',
        hidden: 0,
        status: 1,
      };
    },

    nameUniqueValidator(rule, value, callback) {
      let param = {name: value, parentId: this.form.parentId ? this.form.parentId : 0};
      if(this.operType=='update')param.id = this.form.id;
      let uniqueCode = this.form.menuType == '2' && param.parentId ? 'menuBtName' : 'menuName';
      this.checkUniqueAsyn(value, callback, uniqueCode, param);
    },

    pathUniqueValidator(rule, value, callback) {
      if(this.form.pathType != '0'){
        callback()
        return;
      }
      let param = {path: value, parentId: this.form.parentId ? this.form.parentId : 0};
      if(this.operType=='update')param.id = this.form.id;
      this.checkUniqueAsyn(value, callback, 'menuPath', param);
    },

    menuTypeChange(newVal){
      this.parentMenuType = newVal==2?"0,1":"0";
    },
    // 选择图标
    selected(name) {
      this.form.icon = name;
    },

    /** 查询菜单下拉列表 */
    loadMenuTree(neId){
      menuTreeSelect({menuType: this.parentMenuType, neId: neId}).then(res => {
        this.treeData = res.data;
      });
    },

    /** 打开弹窗 */
    openDialog(title, operType, proms) {
      proms = proms instanceof Promise ? proms : new Promise(resolve=>resolve({data:proms}));
      proms.then(res => {
        this.parentMenuType = res.data && res.data.menuType == 2? "0,1" : "0";
        this.loadMenuTree(res.data.id)
        res.data.parentId = res.data.parentId == 0 ? null : res.data.parentId;
        this.openD({
          form: res.data,
          operType: operType,
          title: title
        });
      });
      //此方法可以在具体的组件里面进行覆盖，可以先调用this.openD(op)方法之后，然后再写自己的具体逻辑
    },

    /** 提交按钮 */
    submitForm(){
      let data = Object.assign({}, this.form);
      data.parentId = !data.parentId?'0':data.parentId;
      this.saveForm(this.url.base, data);
    }

  }
};
</script>
